package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.QueryInterceptor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Apply;
import com.wanmait.schedule.entity.Company;
import com.wanmait.schedule.entity.CompanyType;
import com.wanmait.schedule.mapper.CompanyMapper;
import com.wanmait.schedule.mapper.CompanyTypeMapper;
import com.wanmait.schedule.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.schedule.service.CompanyTypeService;
import com.wanmait.schedule.util.Result;
import com.wanmait.schedule.service.UserinfoService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

//1
//    2


    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private CompanyTypeService companyTypeService;
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private CompanyTypeMapper companyTypeMapper;

    @Override
    public PageInfo<Company> findPage(Integer pageNum, Integer papgeSize) {//不用这个方法
        PageHelper.startPage(pageNum,papgeSize);
        List<Company> companies = this.list();
        companies.forEach(company -> {
            company.setCompanyType(companyTypeService.getById(company.getCompanyTypeId()));
            company.setUserinfo(userinfoService.getById(company.getUserinfoId()));
        });
        PageInfo<Company> pageInfo = new PageInfo<>(companies);
        return pageInfo;
    }

    @Override
    public Page<Company> companys(Integer Page,Integer Number) {//第二种办法
        Page<Company> page = new Page<>(Page,Number);
        companyMapper.selectPage(page,null);
        page.getRecords().forEach(company -> {
            company.setCompanyType(companyTypeService.getById(company.getCompanyTypeId()));
            company.setUserinfo(userinfoService.getById(company.getUserinfoId()));
        });
        return page;
    }

    @Override
    public List<Company> selectCompang(String keyword) {//通过关键字找到企业
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.like("name",keyword);
        List<Company> companies = companyMapper.selectList(wrapper);
        companies.forEach(company -> {
            company.setCompanyType(companyTypeService.getById(company.getCompanyTypeId()));
            company.setUserinfo(userinfoService.getById(company.getUserinfoId()));
        });
        return companies;
    }

    @Override
    public Company initData(Integer companyId) {
        Company company = companyMapper.selectById(companyId);
        company.setCompanyType(companyTypeService.getById(company.getCompanyTypeId()));
        return company;
    }

    @Override
    public List<CompanyType> initCompanyType() {
        LambdaQueryWrapper<CompanyType> queryWrapper = new LambdaQueryWrapper<>();
        List<CompanyType> companyTypes = companyTypeMapper.selectList(queryWrapper);
        return companyTypes;
    }

    @Override
    public Boolean companyEdit(Company company, MultipartFile file, Integer companyId) {
        if(file != null && !file.isEmpty()){
            if(file.getSize()>1024*1024*2){
                return false;
            }
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            if (!",jpg,jpeg,webp,png,".contains(","+ext.toLowerCase()+",")){
                return false;
            }
            try {
                String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadFiles/companyLogo";
                String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ext;
                file.transferTo(new File(path,fileName));
                company.setLogo(fileName);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        LambdaUpdateWrapper<Company> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Company::getId,companyId);
        companyMapper.update(company,updateWrapper);
        return true;
    }


}
